﻿using System;
using System.Collections.Generic;
using System.Text;

namespace CJJ_BlogApiV3.Model
{
    /// <summary>
    /// 公共返回类
    /// </summary>
    public class Result
    {
        /// <summary>
        /// 是否成功
        /// </summary>
        public bool IsSucceed { get; set; }

        /// <summary>
        /// 失败原因
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        ///  状态码. 200 = 成功, 401 = 未授权，402=参数错误, 500 = 未知错误, 501 = 系统错误, 701 = 未登录或登录超期
        /// </summary>
        public int ErrorCode { set; get; }


        /// <summary>
        /// 返回一个成功的操作结果
        /// </summary>
        public static Result Success()
        {
            return new Result()
            {
                IsSucceed = true,
                Message = "操作成功",
                ErrorCode = 200
            };
        }

        /// <summary>
        /// 返回一个失败的操作结果
        /// </summary>
        /// <param name="message"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static Result Failure(string message = "操作失败", int code = 501)
        {
            return new Result()
            {
                IsSucceed = false,
                Message = message,
                ErrorCode = code
            };
        }
    }


    /// <summary>
    /// API接口调用返回结果
    /// </summary>
    public class Result<T> : Result
    {
        /// <summary>
        /// API接口返回结果
        /// </summary>
        public T Data { get; set; }

        /// <summary>
        /// 构造一个默认的调用结果
        /// </summary>
        public Result()
        {
            IsSucceed = false;
            Message = string.Empty;
            Data = default(T);
        }

        /// <summary>
        /// 获取一个操作成功返回结构对象
        /// </summary>
        public static Result<T> Success(T obj)
        {
            return new Result<T>()
            {
                Data = obj,
                IsSucceed = true,
                Message = "成功",
                ErrorCode = 0
            };
        }
        /// <summary>
        /// 返回一个失败的操作结果
        /// </summary>
        /// <param name="message"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public new static Result<T> Failure(string message = "操作失败", int code = 501)
        {
            return new Result<T>()
            {
                IsSucceed = false,
                Message = message,
                ErrorCode = code
            };
        }
    }
}
